package com.freecloud.javabasics.algorithm.random;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;

import java.util.Map;

/**
 * 验证随机数在0-100之间出现的概率
 *
 * @Author: maomao
 * @Date: 2021/8/20 4:45 下午
 */
public class RandomNumber {

    public static void main(String[] args) {

        Map<Integer, Integer> allMap = CollUtil.newHashMap(100);
        Integer val;
        int totalNum = 100000;
        for (int i = 0; i < totalNum; i++) {
            int randomNumber = RandomUtil.randomInt(0, 100);
            val = allMap.get(randomNumber);
            if (ObjectUtil.isNull(val)) {
                val = 0;
            }
            val++;
            allMap.put(randomNumber, val);
        }
        Integer sum = 0;

        for (Map.Entry<Integer, Integer> entity : allMap.entrySet()) {
            System.out.println(String.format("%s : %s", entity.getKey(), entity.getValue()));
            sum += entity.getValue();
        }
        System.out.println(String.format("sum : %s", sum));
    }

}
